package com.managerProvider.service.impl;

import com.managerProvider.mapper.StoreMapper;
import entity.ResponseMessage;
import entity.StatusCode;
import entity.Store;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import service.StoreService;

import java.util.List;

/**
 * 门店服务impl
 * The type Store service.
 *
 * @author tapoliuliyue
 * @date 2022/04/23
 */
@Service
public class StoreServiceImpl implements StoreService {
    @Autowired
    private StoreMapper storeMapper;

    private Logger logger = Logger.getLogger(this.getClass());

    @Override
    public ResponseMessage findAllStore(Store store) {
        ResponseMessage msg = new ResponseMessage();
        try {
            List<Store> stores = storeMapper.selectAllStore(store);
            if (stores == null || stores.size() == 0) {
                msg.setCode(StatusCode.ERROR);
                msg.setFlag(false);
                msg.setMessage("find all store is empty");
                logger.debug("StoreServiceImpl findAllStore params:" + store + ",result:" + stores);
                return msg;
            }
            msg.setCode(StatusCode.OK);
            msg.setData(stores);
            msg.setFlag(true);
            msg.setMessage("ok");
            logger.debug("StoreServiceImpl findAllStore params:" + store + ",result:" + stores);
            return msg;
        } catch (Exception e) {
            msg.setCode(StatusCode.ERROR);
            msg.setFlag(false);
            msg.setMessage("find all store is error");
            logger.error("StoreServiceImpl findAllStore params:" + store + ",result:" + e);
            e.printStackTrace();
            return msg;
        }
    }

    @Override
    public ResponseMessage updateStore(Store store) {
        ResponseMessage msg = new ResponseMessage();
        try {
            int update = storeMapper.updateById(store);
            if (update <= 0) {
                msg.setCode(StatusCode.ERROR);
                msg.setFlag(false);
                msg.setMessage("update store failed");
                logger.debug("StoreServiceImpl updateStore params:" + store + ",result:" + update);
                return msg;
            }
            msg.setCode(StatusCode.OK);
            msg.setData(update);
            msg.setFlag(true);
            msg.setMessage("ok");
            logger.debug("StoreServiceImpl updateStore params:" + store + ",result:" + update);
            return msg;
        } catch (Exception e) {
            msg.setCode(StatusCode.ERROR);
            msg.setFlag(false);
            msg.setMessage("updateStore is error");
            logger.error("StoreServiceImpl updateStore params:" + store + ",result:" + e);
            e.printStackTrace();
            return msg;
        }
    }

    @Override
    public ResponseMessage deleteStore(Integer id) {
        return null;
    }
}
